#!/bin/bash
###SHELLPACK preamble netpipe-bench 3.7.2

TASKSET_SERVER=
TASKSET_CLIENT=
SERVER_HOST=127.0.0.1
if [ "$REMOTE_SERVER_HOST" != "" ]; then
	SERVER_HOST=$REMOTE_SERVER_HOST
fi

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargBindings
###SHELLPACK parseargEnd
###SHELLPACK monitor_hooks

###SHELLPACK check_install_required netpipe-${VERSION}
###SHELLPACK init_complete

export REMOTE_SERVER_SCRIPT=$SCRIPTDIR/shellpacks/shellpack-bench-netpipe
case $SERVERSIDE_COMMAND in
start)
	echo Killing old servers
	killall NPtcp

	echo Starting server
	mmtests_activity netpipe-$SERVERSIDE_NAME
	monitor_pre_hook $LOGDIR_RESULTS netpipe-$SERVERSIDE_NAME
	NETROOT=$SHELLPACK_SOURCES/netpipe-${VERSION}-installed
	nohup $TASKSET_SERVER $NETROOT/NPtcp 0<&- &>/dev/null &
	sleep 5
	exit $SHELLPACK_SUCCESS
	;;
stop)
	monitor_post_hook $LOGDIR_RESULTS netpipe-$SERVERSIDE_NAME
	echo Killing old server
	killall NPtcp
	exit $SHELLPACK_SUCCESS
	;;
flush)
	exit $SHELLPACK_SUCCESS
	;;
none)
	echo Killing old servers
	killall NPtcp

	echo Starting server
	NETROOT=$SHELLPACK_SOURCES/netpipe-${VERSION}-installed 
	$TASKSET_SERVER $NETROOT/NPtcp &
	sleep 2
	;;
*)
	die Unrecognised server-side command
	;;
esac

echo $PROTOCOL > $LOGDIR_RESULTS/protocols
EXTRA=
if [ "$PROTOCOL" = "UDP_STREAM" ]; then
	EXTRA="-P 15895"
fi

mmtests_server_ctl start --serverside-name netpipe-server
mmtests_activity netpipe
monitor_pre_hook $LOGDIR_RESULTS $P
	eval $TASKSET_CLIENT $NETROOT/NPtcp -h $SERVER_HOST	\
		-o $LOGDIR_RESULTS/netpipe.log			\
			|| die Failed to run netpipe
monitor_post_hook $LOGDIR_RESULTS $P
mmtests_server_ctl stop --serverside-name netpipe-server
killall NPtcp

exit $SHELLPACK_SUCCESS
